﻿using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using Request = MeshVR.AssetLoader.AssetBundleFromFileRequest;

public class Examples : MVRScript
{
    private Dictionary<string, AnimationClip> dicAnims = new Dictionary<string, AnimationClip>();

    public override void Init()
    {
        base.Init();

        // assetbundle加载（与assetbundle无关的UI要在资源加载前创建好，否则会发现UI的参数无法正常初始化）
        //var assetsBundlePath = Utils.GetPluginPath(this) + "/atest.assetbundle"; 
        //Request request = new AssetLoader.AssetBundleFromFileRequest { path = assetsBundlePath, callback = OnAssetBundleLoaded };
        //AssetLoader.QueueLoadAssetBundleFromFile(request);

        // 按键绑定
        //https://github.com/acidbubbles/vam-keybindings

        // VR
        //if (SuperController.singleton.isOVR || SuperController.singleton.isOpenVR) return;

        // "Empty" "Person"
        //if (containingAtom.type != "SessionPluginManager")
        //{
        //    SuperController.LogError("need add on SessionPluginManager!");
        //    return;
        //}

        //morphy 获取与设置
        //JSONStorable storableByID = this.containingAtom.GetStorableByID("geometry");
        //if (storableByID != null)
        //{
        //    DAZCharacterSelector dazcharacterSelector = storableByID as DAZCharacterSelector;
        //    GenerateDAZMorphsControlUI morphsControlUI = dazcharacterSelector.morphsControlUI;
        //    if (morphsControlUI != null)
        //    {
        //        List<DAZMorph> morphs = morphsControlUI.GetMorphs();
        //        this.mouthOpen_morph = morphs.FirstOrDefault(x => x.region == "Pose Controls/Head/Mouth" && x.morphName == "PHMMouthOpen");
        //        if (this.mouthOpen_morph != null)
        //        {
        //            this.mouthOpen_morph.SetValue(0);
        //            this.mouthOpen_morph.isTransient = false;
        //            this.mouthOpen_morph.isRuntime = false;
        //        }
        //        else
        //        {
        //            SuperController.LogMessage("mouth open morph not found");
        //        }
        //    }
        //}
    }

    private void OnAssetBundleLoaded(Request abffr)
    {

    }

    private void ScanAnimations()
    {
        UnityEngine.Resources
           .FindObjectsOfTypeAll(typeof(AnimationClip))
           .Cast<AnimationClip>()
           .ToList()
           .ForEach(x =>
           {
               if (!dicAnims.ContainsKey(x.name)) dicAnims.Add(x.name, x);
           });
    }

    private FreeControllerV3 GetController(string name)
    {
        return containingAtom.freeControllers.FirstOrDefault(fc => fc.name == name);
    }
}
